package mapper

import (
	"gin-epg/internal/app/common/page"
	"gin-epg/internal/app/dao"
	"gin-epg/internal/app/entity"
	"gin-epg/internal/app/param"
)

/*
*
获取epgChannel列表
*/
func GetEpgChannelList(params *param.QueryEpgChannelListParam) (epgChannelList []*entity.EpgChannel, err error) {
	query := dao.SqlSession.Model(&entity.EpgChannel{})

	if params.Name != "" {
		query = query.Where("name LIKE ?", "%"+params.Name+"%")
	}

	if params.Province != "" {
		query = query.Where("province = ?", params.Province)
	}

	if err := query.Find(&epgChannelList).Error; err != nil {
		return nil, err
	}
	return
}

/*
* 获取 EpgChannel 分页
 */
func GetEpgChannelPage(params *param.QueryEpgChannelPageParam) (*page.PageResult[entity.EpgChannel], error) {
	query := dao.SqlSession.Model(&entity.EpgChannel{})

	// 添加搜索关键字过滤
	if params.SearchKey != "" {
		query = query.Where("name LIKE ? OR province LIKE ?", "%"+params.SearchKey+"%", "%"+params.SearchKey+"%")
	}

	// 添加名称过滤
	if params.Name != "" {
		query = query.Where("name LIKE ?", "%"+params.Name+"%")
	}

	// 添加省份过滤
	if params.Province != "" {
		query = query.Where("province = ?", params.Province)
	}

	// 添加排序
	if params.SortField != "" && params.SortOrder != "" {
		orderClause := params.SortField + " " + params.SortOrder
		query = query.Order(orderClause)
	}

	// 查询总记录数
	var total int64
	if err := query.Count(&total).Error; err != nil {
		return nil, err
	}

	// 计算总页数
	totalPages := int(total / int64(params.Size))
	if total%int64(params.Size) > 0 {
		totalPages++
	}

	// 计算偏移量
	offset := (params.Current - 1) * params.Size

	// 添加分页
	var epgChannelList []*entity.EpgChannel
	if err := query.Offset(offset).Limit(params.Size).Find(&epgChannelList).Error; err != nil {
		return nil, err
	}

	// 将 []*entity.EpgChannel 转换为 []entity.EpgChannel
	var records []entity.EpgChannel
	for _, epgChannel := range epgChannelList {
		records = append(records, *epgChannel)
	}

	// 创建 PageResult 实例
	pageResult := page.NewPageResult[entity.EpgChannel](records, total, totalPages, params.Current, params.Size)

	return pageResult, nil
}

/*
*
根据id查询用户EpgChannel
*/
func GetEpgChannelById(id string) (epgChannel *entity.EpgChannel, err error) {
	// 初始化 epgChannel 变量
	epgChannel = new(entity.EpgChannel)

	// 执行查询
	if err = dao.SqlSession.Where("id=?", id).First(epgChannel).Error; err != nil {
		return nil, err
	}
	return epgChannel, nil
}

/*
*
根据name查询用户EpgChannel
*/
func GetEpgChannelByName(name string) (epgChannel *entity.EpgChannel, err error) {
	// 初始化 epgChannel 变量
	epgChannel = new(entity.EpgChannel)

	// 执行查询
	if err = dao.SqlSession.Where("name=?", name).First(epgChannel).Error; err != nil {
		return nil, err
	}
	return epgChannel, nil
}

// 创建EpgChannel
func CreateEpgChannel(epgChannel *entity.EpgChannel) (err error) {
	if err = dao.SqlSession.Create(epgChannel).Error; err != nil {
		return err
	}
	return
}

/*
*
更新EpgChannel信息
*/
func UpdateEpgChannel(epgChannel *entity.EpgChannel) (err error) {
	err = dao.SqlSession.Save(epgChannel).Error
	return
}

/*
*
根据id删除epgChannel
*/
func DeleteEpgChannelById(id string) (err error) {
	err = dao.SqlSession.Where("id=?", id).Delete(&entity.EpgChannel{}).Error
	return
}

/*
*
根据name删除epgChannel
*/
func DeleteEpgChannelByName(name string) (err error) {
	err = dao.SqlSession.Where("name=?", name).Delete(&entity.EpgChannel{}).Error
	return
}

/*
*
根据name更新logo字段
*/
func UpdateEpgChannelLogoByName(name string, logo string) (err error) {
	err = dao.SqlSession.Model(&entity.EpgChannel{}).Where("name=?", name).Update("logo", logo).Error
	return
}
